package com.itheima.Test;


public class Test {
    public static void main(String[] args) {
        MilkBox mb = new MilkBox();
        int count = 30;

        Thread sng = new Thread(() -> {
            for (int i = 1; i <= count; i++) {
                synchronized (mb) {
                    if (mb.isFlag()) {
                        try {
                            mb.wait();
                            i--;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        mb.put(i, i);
                        mb.setFlag(true);
                        mb.notifyAll();
                    }
                }
            }
        });
        sng.setDaemon(false);
        sng.start();

        int i = 0;
        Thread sy = new Thread(() -> {
            while (true) {
                synchronized (mb) {
                    if (i > count) {
                        break;
                    }
                    if (mb.isFlag()) {
                        mb.take();
                        mb.setFlag(false);
                        mb.notifyAll();
                    } else {
                        try {
                            mb.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        sy.setDaemon(true);
        sy.start();
    }
}
